package com.ai.academiainteligente;

import org.json.JSONObject;

import com.ai.WS.AIWS;
import com.ai.WS.AIWSTask;
import com.ai.util.AIActivity;
import com.ai.util.AISession;
import com.ai.util.Academia;
import com.ai.util.ModalidadeAdapter;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class AcademiaActivity extends Activity implements AIActivity,
		OnClickListener {

	int idAcademia;
	int idCliente;
	ProgressBar progressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_academia);
		Academia academia = (Academia) getIntent().getSerializableExtra(
				"academia");
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		TextView txtFantasia = (TextView) findViewById(R.id.txtFantasia);
		TextView txtEndereco1 = (TextView) findViewById(R.id.txtEndereco);
		TextView txtEndereco2 = (TextView) findViewById(R.id.txtEndereco2);
		TextView txtFone = (TextView) findViewById(R.id.txtFone);
		TextView txtCnpj = (TextView) findViewById(R.id.txtCnpj);
		txtFantasia.setText(academia.getFantasia());
		txtEndereco1.setText(academia.getEndereco() + ", "
				+ academia.getNumero());
		txtEndereco2.setText(academia.getBairro() + " - "
				+ academia.getCidade() + " - " + academia.getEstado());
		txtFone.setText(academia.getTelefone());
		txtCnpj.setText(academia.getCnpj());
		Button btnAssociar = (Button) findViewById(R.id.btnAssociar);
		btnAssociar.setOnClickListener(this);
		idCliente = AISession.usuario.getId();
		idAcademia = academia.getId();
		ListView listView = (ListView) findViewById(R.id.ltsModalidades);
		listView.setAdapter(new ModalidadeAdapter(academia.getModalidades()));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.academia, menu);
		return true;
	}

	@Override
	public void onClick(View view) {
		if (view.getId() == R.id.btnAssociar) {
			AIWSTask conserveTask = new AIWSTask(this,
					AIWS.metodos.AssociarAcademia, progressBar);
			try {
				conserveTask.execute(idAcademia + "", idCliente + "");
			} catch (Exception e) {
				conserveTask.cancel(true);
			}
		}

	}

	@Override
	public void retornoWS(JSONObject json) {

		Toast.makeText(this, "Associado com sucesso!", Toast.LENGTH_LONG)
				.show();
	}

	@Override
	public Activity getActivity() {
		return this;
	}

}
